package tech.abing.spider.task.pageprocessor.amac;

import java.sql.Timestamp;
import java.util.List;
import java.util.UUID;
import java.util.regex.Pattern;

import org.apache.log4j.Logger;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import tech.abing.spider.component.PageProcessor;
import tech.abing.spider.component.PageScript;
import tech.abing.spider.model.Proccessable;
import tech.abing.spider.page.OkPage;
import tech.abing.spider.page.Page;
import tech.abing.spider.request.BasicRequest;
import tech.abing.spider.request.PageRequest;
import tech.abing.spider.request.StartContext;
import tech.abing.spider.request.PageRequest.Method;
import tech.abing.spider.request.PageRequest.PostType;
import tech.abing.spider.task.model.amac.AdminLicenseEntity;
import tech.abing.spider.task.model.amac.AssetSupportSpecialEntity;
import tech.abing.spider.task.utils.TaskUtil;
import tech.abing.spider.util.DateUtils;
import tech.abing.spider.util.StringUtils;
import tech.abing.spider.util.UuIdPublic;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

public class AssetSupportSpecialProcessor implements PageProcessor {

	private static Logger logger = Logger.getLogger(AssetSupportSpecialProcessor.class);
	
	

	@Override
	public PageScript getJavaScript() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Pattern getNormalContain() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void process(OkPage page, StartContext context,
			List<BasicRequest> queue, List<Proccessable> objectContainer)
			throws Exception {
		JSONObject jsonObj = JSONObject.fromObject(page.getContent());
		boolean hasPre = jsonObj.getBoolean("hasPre");
	
		if(!hasPre){//第一页
			long totalPages = jsonObj.getLong("totalPages");
			long totalCount = jsonObj.getLong("totalCount");
			PageRequest request = page.getRequest();
			if(totalCount>1){
				for (int i = 2; i <= totalPages; i++) {
					PageRequest req = context.createPageRequest(request.getUrl(), AssetSupportSpecialProcessor.class);
					
					req.setMethod(request.getMethod());
					req.setPostType(request.getPostType());
					
					req.setHeaders(request.getHeaders());
					
					req.putParams("nd", TaskUtil.getRandomStrByDate());
					req.putParams("page.pageSize", "50");
					req.putParams("page.pageNo", String.valueOf(i));
					
					req.putParams("page.sqlCKey", "SIZE_ABS_PUBLICITY_WEB");
					req.putParams("_search", "false");
					req.putParams("page.orderBy", "AT_AUDIT_DATE");
					req.putParams("page.order", "desc");
					req.putParams("filter_LIKES_ASPI_NAME", "");
					req.putParams("filter_GES_AT_AUDIT_DATE", "");
					req.putParams("filter_LES_AT_AUDIT_DATE", "");
					req.putParams("page.searchFileName", "publicity_abs_web");
					req.putParams("page.sqlKey", "PAGE_ABS_PUBLICITY_WEB");
					queue.add(req);
				}
			}
		}
		
		//解析数据
		JSONArray result = jsonObj.getJSONArray("result");
		if(result != null && result.size() > 0){
			for(int i = 0; i < result.size(); i++){
				JSONObject retObj = result.getJSONObject(i);
				AssetSupportSpecialEntity assetSupport = new AssetSupportSpecialEntity();
				assetSupport.setId(UuIdPublic.timeStrongID(5));
				assetSupport.setAspiName(retObj.getString("ASPI_NAME"));
				assetSupport.setAspiNamePassTime(DateUtils.parseDate(retObj.getString("AT_AUDIT_DATE")));
				assetSupport.setAspiNO(retObj.getString("ASPI_BA_NUMBER"));
				assetSupport.setCreateTime(new Timestamp(System.currentTimeMillis()));
				assetSupport.setManagerName(retObj.getString("ASPI_GL_NAME"));
				assetSupport.setCustodian(retObj.getString("AII_TGR"));
				assetSupport.setSerialNumber(retObj.getString("RN"));
				StringUtils.removeSpace(assetSupport);
				objectContainer.add(assetSupport);
			}
		}
	}
	



	@Override
	public void processErrorPage(Page page, StartContext context,
			List<Proccessable> objectContainer) throws Exception {
		// TODO Auto-generated method stub
		
	}
	

}